/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hnilice.excel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import org.apache.commons.collections.comparators.NullComparator;

/**
 *
 * @author swosh
 */
public class TerminCisOri implements Serializable, Comparable<TerminCisOri> {

    private String nazUlice;
    private Date terCis;
    private String nazAkce;
    private int[] cisOrientacni;
    static Comparator<String> nullSafeComparator = new NullComparator(String.CASE_INSENSITIVE_ORDER);

    public TerminCisOri() {
    }

    public TerminCisOri(String nazUlice) {
        this.nazUlice = nazUlice;
    }

    public TerminCisOri(String nazUlice, Date terCis) {
        this.nazUlice = nazUlice;
        this.terCis = terCis;
    }

    public TerminCisOri(String nazUlice, Date terCis, String nazAkce) {
        this.nazUlice = nazUlice;
        this.terCis = terCis;
        this.nazAkce = nazAkce;
    }

    public TerminCisOri(String nazUlice, Date terCis, String nazAkce, int[] cisOrientacni) {
        this.nazUlice = nazUlice;
        this.terCis = terCis;
        this.nazAkce = nazAkce;
        this.cisOrientacni = cisOrientacni;
    }

    public String getNazUlice() {
        return nazUlice;
    }

    public void setNazUlice(String nazUlice) {
        this.nazUlice = nazUlice;
    }

    public Date getTerCis() {
        return terCis;
    }

    public void setTerCis(Date terCis) {
        this.terCis = terCis;
    }

    public String getNazAkce() {
        return nazAkce;
    }

    public void setNazAkce(String nazAkce) {
        this.nazAkce = nazAkce;
    }

    public int[] getCisOrientacni() {
        return cisOrientacni;
    }

    public void setCisOrientacni(int[] cisOrientacni) {
        this.cisOrientacni = cisOrientacni;
    }

    /**
     * TODO CHANGED
     * Comparator for TerminCisOri. At the first compare terms of cleaning (terCis). On the bottom
     * (lesser ranking)  puts earlier date. Year 2000 is lesser then 1900. If dates is same then
     * sorts naem of street lixicongraphiclly by using String method compareToIgnoreCase.
     *
     * @param o TerminCisOri
     * @return int 1 before, 0 equals, -1 after or null
     */
    public int compareTo(TerminCisOri o) {

        int result = nullSafeComparator.compare(this.getNazAkce(), o.getNazAkce());
        if (result > 0) {
            return 1;
        } else if (result < 0) {
            return -1;
        } else {
            result = nullSafeComparator.compare(this.getNazUlice(), o.getNazUlice());
            if (result > 0) {
                return 1;
            } else if (result < 0) {
                return -1;
            } else {
                if (this.getCisOrientacni() == null && o.getCisOrientacni() != null) {
                    return 1;
                }
                if (this.getCisOrientacni() != null && o.getCisOrientacni() == null) {
                    return -1;
                }
                if (this.getCisOrientacni() == null && o.getCisOrientacni() == null) {
                    return this.compareToTerCis(o);
                }
                int[] thisCisOri = this.getCisOrientacni();
                int[] oCisOri = o.getCisOrientacni();
                Arrays.sort(thisCisOri);
                Arrays.sort(oCisOri);
                int minThis = thisCisOri[0];
                int minO = oCisOri[0];
                if (minThis > minO) {
                    return 1;
                } else if (minThis < minO) {
                    return -1;
                } else {
                    return this.compareToTerCis(o);
                }
            }
        }
    }

    private int compareToTerCis(TerminCisOri o) {
        int result;
        if (this.getTerCis() == null && o.getTerCis() != null) {
            return 1;
        }
        if (this.getTerCis() != null && o.getTerCis() == null) {
            return -1;
        }
        if (this.getTerCis() == null && o.getTerCis() == null) {
            return 0;
        }
        result = this.getTerCis().compareTo(o.getTerCis());
        if (result > 0) {
            return 1;
        } else if (result < 0) {
            return -1;
        } else {
            return 0;
        }

    }

    @Override
    public String toString() {
        StringBuilder sB = new StringBuilder();
        if (nazAkce != null) {
            sB.append(nazAkce);
        } else {
            sB.append("null");
        }
        sB.append(", ");
        if (nazUlice != null) {
            sB.append(nazUlice);
        } else {
            sB.append("null");
        }
        sB.append(", ");
        if (cisOrientacni != null) {
            for (int i = 0; i < cisOrientacni.length; i++) {
                int j = cisOrientacni[i];
                if (i == 0) {
                    sB.append(j);
                } else {
                    sB.append(", ").append(j);
                }
            }
        } else {
            sB.append("null");
        }
        sB.append(", ");
        if (terCis != null) {
            sB.append(terCis.toString());
        } else {
            sB.append("null");
        }
        sB.append(". ");
        return sB.toString();
    }

}
